package com.gable.report.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.gable.report.common.util.ChartsExportExcel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * Created by lvdouStart on 2017/7/4.
 * 政府报表excel导出类
 */
@Controller
@RequestMapping("charts")
public class ChartsExportController {

    private static Log logger = LogFactory.getLog(ChartsExportController.class);

   /* @RequestMapping(value = "/excelexport")
    public String excelExport(HttpServletRequest request, HttpServletResponse response,
                                        @RequestParam String fileName,String thead,@RequestParam String tbody){
        try {

            //表头解析成数组
            List<String> array = JSON.parseArray(tbody,String.class);
            List<List<String>> tableList = new ArrayList<>();
            if(array != null && array.size() != 0){
                if(array.get(0).indexOf("[") == -1){
                    tableList.add(array);
                }else{
                    for(int i=0;i<array.size();i++){
                        List<String> subList = JSON.parseArray(array.get(i),String.class);
                        tableList.add(subList);
                    }
                }
                ChartsExportExcel.export(tableList,fileName,response);
            }

        } catch (Exception e) {
            e.printStackTrace();
            logger.info("ReportExportController: "+ "导出异常");
            return null;
        }
        return null;
    }*/

    /**
     * @Author: 杨青桦
     * @Description: excel导出重写（添加单元格合并excel导出）
     * @Date: 2018/1/10 13:10
     * @param request
     * @param response
     * @param fileName 文件名
     * @param thead 表头
     * @param tbody 数据
     * @param tfoot 表尾 总计
     * @return
     */
    @RequestMapping(value = "/excelexport")
    public String excelExport(HttpServletRequest request, HttpServletResponse response,
                              @RequestParam String fileName,@RequestParam String thead,@RequestParam String tbody,
                                           String tfoot){
        try {

            //表头解析成数组
            List<String> headList = JSON.parseArray(thead,String.class);
            List<String> footList = JSON.parseArray(tfoot,String.class);
            Map<String,List<JSONArray>> tempMap = JSON.parseObject(tbody,LinkedHashMap.class);
            //约定无须合并单元格的key为"0",需要的为key为一级68分类名称
            if(!CollectionUtils.isEmpty(tempMap)){
                if(tempMap.containsKey("0")){
                    List<List<String>> bodyList = new ArrayList<>();
                    bodyList.add(headList); //表头
                    List<JSONArray> arrayList = tempMap.get("0");
                    if(!CollectionUtils.isEmpty(arrayList)){
                       for(JSONArray array : arrayList){
                           bodyList.add((List<String>)array.toJavaObject(List.class));
                       }
                    }
                    bodyList.add(footList); //表尾
                    ChartsExportExcel.export(bodyList,fileName,response);
                }else{
                    Map<String,List<List<String>>> bodyMap = new LinkedHashMap<>();
                    //分块处理
                    for (Map.Entry<String,List<JSONArray>> entry : tempMap.entrySet()) {
                        List<List<String>> list = new ArrayList<>();
                        for(JSONArray array : entry.getValue()){
                            list.add((List<String>)array.toJavaObject(List.class));
                        }
                        bodyMap.put(entry.getKey(),list);
                    }
                    ChartsExportExcel.export(bodyMap,fileName,response,headList,footList);
                }
            }



        } catch (Exception e) {
            e.printStackTrace();
            logger.info("ReportExportController: "+ "导出异常");
            return null;
        }


        return null;
    }
}
